// var VSHADER_SOURCE = 'attribute vec4 a_Position;\n' +
// 'uniform float u_CosB, u_SinB;\n' +
// 'void main() {\n' +
// '  gl_Position.x = a_Position.x * u_CosB ;\n' +
// '  gl_Position.y = a_Position.x * u_SinB ;\n' +
// '  gl_Position.z = a_Position.z;\n' +
// '  gl_Position.w = 1.0;\n' +
// '}\n';
// sin(x + y) = sin x * cos y + cos x * sin y;
var VSHADER_SOURCE = `
    attribute vec4 a_Position;
    uniform mat4 u_xformMatrix;
    void main(){
      gl_Position =  u_xformMatrix * a_Position;
      // gl_PointSize = 10.0;
    }
  `
var FSHADER_SOURCE = `
    precision mediump float;
    uniform vec4 u_Color;
    void main() {
      // gl_FragColor = u_Color;
      gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
    }
  `;
var canvas = document.getElementById('example');
var gl = getWebGLContext(canvas);

function clear() {
  // Specify the color for clearing <canvas>
  gl.clearColor(0.0, 0.0, 0.0, 1.0);
  // Clear <canvas>
  gl.clear(gl.COLOR_BUFFER_BIT);
}

function initVertexBuffers(gl) {
  var vertices = new Float32Array([
    0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5
  ]);
  var n = 3; // The number of vertices
  var vertexBuffer = gl.createBuffer();
  var a_Position = gl.getAttribLocation(gl.program, 'a_Position');

  if (!vertexBuffer) {
    console.log('Failed to create the buffer object');
    return -1;
  }

  if (a_Position < 0) {
    console.log('Failed to get the storage location of a_Position');
    return -1;
  }
  console.log("vertexBuffer", vertexBuffer)
  // Bind the buffer object to target
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);

  // Write date into the buffer object
  gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

  // Assign the buffer object to a_Position variable
  gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);

  // Enable the assignment to a_Position variable
  gl.enableVertexAttribArray(a_Position);

  return n;
}

function initMatrixBuffers(gl){
  var ANGLE = 0.0;
  // // Pass the data required to rotate the shape to the vertex shader
  var radian = Math.PI * ANGLE / 180.0; // Convert to radians
  var cosB = Math.cos(radian);
  var sinB = Math.sin(radian);
  var tx = 0.4,ty = 0.0;
  var sx = sy = 0.2 ,sz = 1
  var xformMatrix = new Float32Array([
    cosB * sx, sinB, 0.0, 0.0,
    -sinB, cosB * sy, 0.0, 0.0,
      0.0,  0.0, 1.0, 0.0,
      tx,  ty, 0.0, 1.0
 ]);
  console.log("cosB",cosB);
  console.log("sinB",sinB);

  // Pass the rotation matrix to the vertex shader
  var u_xformMatrix = gl.getUniformLocation(gl.program, 'u_xformMatrix');
  if (!u_xformMatrix) {
    console.log('Failed to get the storage location of u_xformMatrix');
    return;
  }
  gl.uniformMatrix4fv(u_xformMatrix, false, xformMatrix);
}


function init() {
  // Initialize shaders
  if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
    console.log('Failed to intialize shaders.');
    return;
  }

  if (initVertexBuffers(gl) < 0) {
    console.log('Failed to set the positions of the vertices');
    return;
  }
  initMatrixBuffers(gl)

  clear();
  // Draw three points
  gl.drawArrays(gl.TRIANGLE_FAN, 0, 4);
}

function main() {
  console.log("main")
  console.log(gl)
  init();
}